{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pandas层次化索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 创建多层行索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1) 隐式构造\n",
    "\n",
    "最常见的方法是给DataFrame构造函数的index参数传递两个或更多的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>27</td>\n",
       "      <td>35</td>\n",
       "      <td>8</td>\n",
       "      <td>69</td>\n",
       "      <td>95</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>34</td>\n",
       "      <td>60</td>\n",
       "      <td>86</td>\n",
       "      <td>83</td>\n",
       "      <td>22</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>94</td>\n",
       "      <td>24</td>\n",
       "      <td>98</td>\n",
       "      <td>50</td>\n",
       "      <td>71</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>71</td>\n",
       "      <td>36</td>\n",
       "      <td>28</td>\n",
       "      <td>48</td>\n",
       "      <td>80</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>35</td>\n",
       "      <td>48</td>\n",
       "      <td>54</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>52</td>\n",
       "      <td>37</td>\n",
       "      <td>35</td>\n",
       "      <td>65</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   27  35   8  69  95  30\n",
       "   李四   34  60  86  83  22  11\n",
       "   王五   94  24  98  50  71  97\n",
       "2班 鲁班   71  36  28  48  80  46\n",
       "   张三丰  35  48  54   9   1   9\n",
       "   张无忌  52  37  35  65   6   7"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = [\n",
    "    ['1班',  '1班',   '1班',  '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '鲁班', '张三丰', '张无忌']\n",
    "]\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Series也可以创建多层索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     34\n",
       "    李四     65\n",
       "    王五     60\n",
       "2班  鲁班     49\n",
       "    张三丰    20\n",
       "    张无忌    27\n",
       "dtype: int32"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=6)\n",
    "\n",
    "index = [\n",
    "    ['1班',  '1班',   '1班',  '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '鲁班', '张三丰', '张无忌']\n",
    "]\n",
    "\n",
    "s = pd.Series(data=data, index=index)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2) 显示构造pd.MultiIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>5</td>\n",
       "      <td>30</td>\n",
       "      <td>4</td>\n",
       "      <td>32</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>75</td>\n",
       "      <td>0</td>\n",
       "      <td>86</td>\n",
       "      <td>34</td>\n",
       "      <td>26</td>\n",
       "      <td>69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>50</td>\n",
       "      <td>73</td>\n",
       "      <td>33</td>\n",
       "      <td>41</td>\n",
       "      <td>98</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>29</td>\n",
       "      <td>63</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>48</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>27</td>\n",
       "      <td>2</td>\n",
       "      <td>43</td>\n",
       "      <td>40</td>\n",
       "      <td>35</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>50</td>\n",
       "      <td>28</td>\n",
       "      <td>11</td>\n",
       "      <td>38</td>\n",
       "      <td>55</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三    5  30   4  32   8   1\n",
       "   李四   75   0  86  34  26  69\n",
       "   王五   50  73  33  41  98  10\n",
       "2班 鲁班   29  63  14  23  48  65\n",
       "   张三丰  27   2  43  40  35  45\n",
       "   张无忌  50  28  11  38  55  37"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = pd.MultiIndex.from_arrays( [\n",
    "    ['1班',  '1班',   '1班',  '2班', '2班', '2班'],\n",
    "    ['张三', '李四', '王五', '鲁班', '张三丰', '张无忌']\n",
    "])\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用tuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>81</td>\n",
       "      <td>45</td>\n",
       "      <td>86</td>\n",
       "      <td>70</td>\n",
       "      <td>10</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>57</td>\n",
       "      <td>67</td>\n",
       "      <td>36</td>\n",
       "      <td>10</td>\n",
       "      <td>18</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>26</td>\n",
       "      <td>49</td>\n",
       "      <td>41</td>\n",
       "      <td>39</td>\n",
       "      <td>20</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>66</td>\n",
       "      <td>46</td>\n",
       "      <td>78</td>\n",
       "      <td>5</td>\n",
       "      <td>87</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>62</td>\n",
       "      <td>10</td>\n",
       "      <td>32</td>\n",
       "      <td>27</td>\n",
       "      <td>97</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>28</td>\n",
       "      <td>60</td>\n",
       "      <td>23</td>\n",
       "      <td>46</td>\n",
       "      <td>37</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   81  45  86  70  10   2\n",
       "   李四   57  67  36  10  18   2\n",
       "   王五   26  49  41  39  20  60\n",
       "2班 鲁班   66  46  78   5  87  25\n",
       "   张三丰  62  10  32  27  97  11\n",
       "   张无忌  28  60  23  46  37  66"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = pd.MultiIndex.from_tuples(\n",
    "    (\n",
    "        ('1班', '张三'), ('1班', '李四'), ('1班', '王五'),\n",
    "        ('2班', '鲁班'), ('2班', '张三丰'), ('2班', '张无忌')\n",
    "    )\n",
    ")\n",
    "\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 使用product\n",
    "    - 笛卡尔积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>82</td>\n",
       "      <td>15</td>\n",
       "      <td>48</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>19</td>\n",
       "      <td>75</td>\n",
       "      <td>72</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>99</td>\n",
       "      <td>27</td>\n",
       "      <td>74</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>21</td>\n",
       "      <td>54</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>65</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>44</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>85</td>\n",
       "      <td>31</td>\n",
       "      <td>69</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  82  15  48  35  24  50\n",
       "   李四  50  75  19  75  72  10\n",
       "   王五  66  99  27  74  16  72\n",
       "2班 张三  20  30  35  21  54  22\n",
       "   李四  93  65  11   3  44  11\n",
       "   王五  31   7  85  31  69  55"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "# 笛卡尔积: {a, b} {c, d} => {a, c}, {a, d}, {b, c}, {b, d}\n",
    "\n",
    "index = pd.MultiIndex.from_product( [\n",
    "    ['1班',  '2班'],\n",
    "    ['张三', '李四', '王五']\n",
    "])\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 多层列索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除了行索引index，列索引columns也能用同样的方法创建多层索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. 多层索引对象的索引与切片操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1）Series的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 对于Series来说，直接中括号[]与使用.loc()完全一样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(1) 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     34\n",
       "    李四     65\n",
       "    王五     60\n",
       "2班  鲁班     49\n",
       "    张三丰    20\n",
       "    张无忌    27\n",
       "dtype: int32"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\524077654.py:14: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[0]\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\524077654.py:15: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  s[1]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1班  李四    65\n",
       "    王五    60\n",
       "dtype: int32"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显式索引\n",
    "s['1班']\n",
    "s.loc['1班']\n",
    "\n",
    "s[['1班']]\n",
    "s[['1班', '2班']]\n",
    "\n",
    "s['1班']['张三']\n",
    "s.loc['1班']['张三']\n",
    "s.loc['1班', '张三']\n",
    "s['1班', '张三']\n",
    "\n",
    "# 隐式索引\n",
    "s[0]\n",
    "s[1]\n",
    "s.iloc[1]\n",
    "s.iloc[[1, 2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(2) 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     34\n",
       "    李四     65\n",
       "    王五     60\n",
       "2班  鲁班     49\n",
       "    张三丰    20\n",
       "    张无忌    27\n",
       "dtype: int32"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  李四     65\n",
       "    王五     60\n",
       "2班  鲁班     49\n",
       "    张三丰    20\n",
       "dtype: int32"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "# 显式切片\n",
    "s['1班' : '2班']\n",
    "s.loc['1班' : '2班']\n",
    "s.loc['1班'][:]\n",
    "\n",
    "# 建议使用隐式索引\n",
    "s[1 : 5]\n",
    "s.iloc[1 : 5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2）DataFrame的操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(1) 索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>82</td>\n",
       "      <td>15</td>\n",
       "      <td>48</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>19</td>\n",
       "      <td>75</td>\n",
       "      <td>72</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>99</td>\n",
       "      <td>27</td>\n",
       "      <td>74</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>21</td>\n",
       "      <td>54</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>65</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>44</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>85</td>\n",
       "      <td>31</td>\n",
       "      <td>69</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  82  15  48  35  24  50\n",
       "   李四  50  75  19  75  72  10\n",
       "   王五  66  99  27  74  16  72\n",
       "2班 张三  20  30  35  21  54  22\n",
       "   李四  93  65  11   3  44  11\n",
       "   王五  31   7  85  31  69  55"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>李四</th>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>19</td>\n",
       "      <td>75</td>\n",
       "      <td>72</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>21</td>\n",
       "      <td>54</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>65</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>44</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>99</td>\n",
       "      <td>27</td>\n",
       "      <td>74</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 李四  50  75  19  75  72  10\n",
       "2班 张三  20  30  35  21  54  22\n",
       "   李四  93  65  11   3  44  11\n",
       "1班 王五  66  99  27  74  16  72"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列索引\n",
    "df['期中']\n",
    "df['期中'][['数学']]\n",
    "df['期中']['数学']\n",
    "df['期中', '数学']\n",
    "df.期中.数学\n",
    "\n",
    "df.iloc[:, 2]\n",
    "df.iloc[:, [0, 2, 1]]\n",
    "df.loc[:, ('期中', '数学')]\n",
    "\n",
    "# # 行索引\n",
    "df.loc['2班']\n",
    "df.loc['2班'].loc['张三']\n",
    "df.loc['2班', '张三']\n",
    "df.loc[('2班', '张三')]\n",
    "\n",
    "df.iloc[1]\n",
    "df.iloc[[1]]\n",
    "df.iloc[[1, 3, 4, 2]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\1751359348.py:3: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  df['期中']['数学']['1班'][0]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "np.int32(15)"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取元素\n",
    "df['期中']['数学']['1班']['张三']\n",
    "df['期中']['数学']['1班'][0]\n",
    "\n",
    "df.iloc[0, 1]\n",
    "df.loc[('1班', '张三'), ('期中', '数学')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(2) 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>82</td>\n",
       "      <td>15</td>\n",
       "      <td>48</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>19</td>\n",
       "      <td>75</td>\n",
       "      <td>72</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>99</td>\n",
       "      <td>27</td>\n",
       "      <td>74</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>21</td>\n",
       "      <td>54</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>65</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>44</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>85</td>\n",
       "      <td>31</td>\n",
       "      <td>69</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  82  15  48  35  24  50\n",
       "   李四  50  75  19  75  72  10\n",
       "   王五  66  99  27  74  16  72\n",
       "2班 张三  20  30  35  21  54  22\n",
       "   李四  93  65  11   3  44  11\n",
       "   王五  31   7  85  31  69  55"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>82</td>\n",
       "      <td>15</td>\n",
       "      <td>48</td>\n",
       "      <td>35</td>\n",
       "      <td>24</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>50</td>\n",
       "      <td>75</td>\n",
       "      <td>19</td>\n",
       "      <td>75</td>\n",
       "      <td>72</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>66</td>\n",
       "      <td>99</td>\n",
       "      <td>27</td>\n",
       "      <td>74</td>\n",
       "      <td>16</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>20</td>\n",
       "      <td>30</td>\n",
       "      <td>35</td>\n",
       "      <td>21</td>\n",
       "      <td>54</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>93</td>\n",
       "      <td>65</td>\n",
       "      <td>11</td>\n",
       "      <td>3</td>\n",
       "      <td>44</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>31</td>\n",
       "      <td>7</td>\n",
       "      <td>85</td>\n",
       "      <td>31</td>\n",
       "      <td>69</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  82  15  48  35  24  50\n",
       "   李四  50  75  19  75  72  10\n",
       "   王五  66  99  27  74  16  72\n",
       "2班 张三  20  30  35  21  54  22\n",
       "   李四  93  65  11   3  44  11\n",
       "   王五  31   7  85  31  69  55"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 行切片\n",
    "df.iloc[1 : 5]\n",
    "df.loc[('1班', '李四') : ('2班', '李四')]\n",
    "df.loc['1班' : '2班']\n",
    "\n",
    "# 列切片\n",
    "df.iloc[:, 1: 5]\n",
    "df.loc[:, '期中': '期末']\n",
    "# df.loc[:, ('期中', '数学') : ('期末', '数学')]  # 报错\n",
    "\n",
    "# 建议切片使用隐式索引：\n",
    "#    使用数字下标来做偏\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 4. 索引的堆叠"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- stack()\n",
    "- unstack()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【小技巧】使用stack()的时候，level等于哪一个，哪一个就消失，出现在行里。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>50</td>\n",
       "      <td>73</td>\n",
       "      <td>43</td>\n",
       "      <td>4</td>\n",
       "      <td>87</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>28</td>\n",
       "      <td>17</td>\n",
       "      <td>88</td>\n",
       "      <td>64</td>\n",
       "      <td>37</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>90</td>\n",
       "      <td>62</td>\n",
       "      <td>60</td>\n",
       "      <td>90</td>\n",
       "      <td>52</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>张三</th>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>89</td>\n",
       "      <td>45</td>\n",
       "      <td>85</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "      <td>45</td>\n",
       "      <td>73</td>\n",
       "      <td>91</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>54</td>\n",
       "      <td>42</td>\n",
       "      <td>76</td>\n",
       "      <td>90</td>\n",
       "      <td>43</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       期中          期末        \n",
       "       语文  数学  英语  语文  数学  英语\n",
       "1班 张三  50  73  43   4  87  80\n",
       "   李四  28  17  88  64  37  65\n",
       "   王五  90  62  60  90  52  93\n",
       "2班 张三  19   3  89  45  85  81\n",
       "   李四  16  58  45  73  91  52\n",
       "   王五  54  42  76  90  43  48"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "# 笛卡尔积: {a, b} {c, d} => {a, c}, {a, d}, {b, c}, {b, d}\n",
    "\n",
    "index = pd.MultiIndex.from_product( [\n",
    "    ['1班',  '2班'],\n",
    "    ['张三', '李四', '王五']\n",
    "])\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\3034055741.py:2: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack()   # 默认是将最里层的列索引变成行索引\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\3034055741.py:3: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack(level=-1)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\3034055741.py:4: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df.stack(level=1)\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_26104\\3034055741.py:6: FutureWarning: The previous implementation of stack is deprecated and will be removed in a future version of pandas. See the What's New notes for pandas 2.1.0 for details. Specify future_stack=True to adopt the new implementation and silence this warning.\n",
      "  df2 = df.stack(level=0)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">1班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>50</td>\n",
       "      <td>73</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>4</td>\n",
       "      <td>87</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">李四</th>\n",
       "      <th>期中</th>\n",
       "      <td>28</td>\n",
       "      <td>17</td>\n",
       "      <td>88</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>64</td>\n",
       "      <td>37</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">王五</th>\n",
       "      <th>期中</th>\n",
       "      <td>90</td>\n",
       "      <td>62</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>90</td>\n",
       "      <td>52</td>\n",
       "      <td>93</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"6\" valign=\"top\">2班</th>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>19</td>\n",
       "      <td>3</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>45</td>\n",
       "      <td>85</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">李四</th>\n",
       "      <th>期中</th>\n",
       "      <td>16</td>\n",
       "      <td>58</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>73</td>\n",
       "      <td>91</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">王五</th>\n",
       "      <th>期中</th>\n",
       "      <td>54</td>\n",
       "      <td>42</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>90</td>\n",
       "      <td>43</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          语文  数学  英语\n",
       "1班 张三 期中  50  73  43\n",
       "      期末   4  87  80\n",
       "   李四 期中  28  17  88\n",
       "      期末  64  37  65\n",
       "   王五 期中  90  62  60\n",
       "      期末  90  52  93\n",
       "2班 张三 期中  19   3  89\n",
       "      期末  45  85  81\n",
       "   李四 期中  16  58  45\n",
       "      期末  73  91  52\n",
       "   王五 期中  54  42  76\n",
       "      期末  90  43  48"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# stack: 将列索引变成行索引\n",
    "df.stack()   # 默认是将最里层的列索引变成行索引\n",
    "df.stack(level=-1)\n",
    "df.stack(level=1)\n",
    "\n",
    "df2 = df.stack(level=0)\n",
    "df2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "【小技巧】使用unstack()的时候，level等于哪一个，哪一个就消失，出现在列里。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"2\" halign=\"left\">语文</th>\n",
       "      <th colspan=\"2\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"2\" halign=\"left\">英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "      <th>1班</th>\n",
       "      <th>2班</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">张三</th>\n",
       "      <th>期中</th>\n",
       "      <td>50</td>\n",
       "      <td>19</td>\n",
       "      <td>73</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>4</td>\n",
       "      <td>45</td>\n",
       "      <td>87</td>\n",
       "      <td>85</td>\n",
       "      <td>80</td>\n",
       "      <td>81</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">李四</th>\n",
       "      <th>期中</th>\n",
       "      <td>28</td>\n",
       "      <td>16</td>\n",
       "      <td>17</td>\n",
       "      <td>58</td>\n",
       "      <td>88</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>64</td>\n",
       "      <td>73</td>\n",
       "      <td>37</td>\n",
       "      <td>91</td>\n",
       "      <td>65</td>\n",
       "      <td>52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">王五</th>\n",
       "      <th>期中</th>\n",
       "      <td>90</td>\n",
       "      <td>54</td>\n",
       "      <td>62</td>\n",
       "      <td>42</td>\n",
       "      <td>60</td>\n",
       "      <td>76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>期末</th>\n",
       "      <td>90</td>\n",
       "      <td>90</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "      <td>93</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       语文      数学      英语    \n",
       "       1班  2班  1班  2班  1班  2班\n",
       "张三 期中  50  19  73   3  43  89\n",
       "   期末   4  45  87  85  80  81\n",
       "李四 期中  28  16  17  58  88  45\n",
       "   期末  64  73  37  91  65  52\n",
       "王五 期中  90  54  62  42  60  76\n",
       "   期末  90  90  52  43  93  48"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unstack: 将行索引变成列索引\n",
    "df2.unstack()\n",
    "df2.unstack(level=-1)\n",
    "df2.unstack(level=2)\n",
    "df2.unstack(level=1)\n",
    "df2.unstack(level=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用fill_value填充"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "      <td>28</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>67</td>\n",
       "      <td>76</td>\n",
       "      <td>33</td>\n",
       "      <td>86</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>53</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>31</td>\n",
       "      <td>14</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>68</td>\n",
       "      <td>56</td>\n",
       "      <td>95</td>\n",
       "      <td>78</td>\n",
       "      <td>16</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>21</td>\n",
       "      <td>17</td>\n",
       "      <td>77</td>\n",
       "      <td>42</td>\n",
       "      <td>67</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>55</td>\n",
       "      <td>23</td>\n",
       "      <td>42</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   56  56  58  28   3  17\n",
       "   李四   67  76  33  86  54  65\n",
       "   王五   53  17  43  31  14  66\n",
       "2班 鲁班   68  56  95  78  16  48\n",
       "   张三丰  21  17  77  42  67  83\n",
       "   张无忌  55  23  42  30  10  22"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0, 100, size=(6, 6))\n",
    "\n",
    "index = pd.MultiIndex.from_tuples(\n",
    "    (\n",
    "        ('1班', '张三'), ('1班', '李四'), ('1班', '王五'),\n",
    "        ('2班', '鲁班'), ('2班', '张三丰'), ('2班', '张无忌')\n",
    "    )\n",
    ")\n",
    "\n",
    "columns = [\n",
    "    ['期中', '期中', '期中', '期末', '期末', '期末'],\n",
    "    ['语文', '数学', '英语', '语文', '数学', '英语']\n",
    "]\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"10\" halign=\"left\">期中</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"10\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"6\" halign=\"left\">语文</th>\n",
       "      <th colspan=\"4\" halign=\"left\">数学</th>\n",
       "      <th>...</th>\n",
       "      <th colspan=\"4\" halign=\"left\">数学</th>\n",
       "      <th colspan=\"6\" halign=\"left\">英语</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>张三</th>\n",
       "      <th>张三丰</th>\n",
       "      <th>张无忌</th>\n",
       "      <th>李四</th>\n",
       "      <th>王五</th>\n",
       "      <th>鲁班</th>\n",
       "      <th>张三</th>\n",
       "      <th>张三丰</th>\n",
       "      <th>张无忌</th>\n",
       "      <th>李四</th>\n",
       "      <th>...</th>\n",
       "      <th>张无忌</th>\n",
       "      <th>李四</th>\n",
       "      <th>王五</th>\n",
       "      <th>鲁班</th>\n",
       "      <th>张三</th>\n",
       "      <th>张三丰</th>\n",
       "      <th>张无忌</th>\n",
       "      <th>李四</th>\n",
       "      <th>王五</th>\n",
       "      <th>鲁班</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1班</th>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>67</td>\n",
       "      <td>53</td>\n",
       "      <td>0</td>\n",
       "      <td>56</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>76</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>17</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>65</td>\n",
       "      <td>66</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2班</th>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>68</td>\n",
       "      <td>0</td>\n",
       "      <td>17</td>\n",
       "      <td>23</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>83</td>\n",
       "      <td>22</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    期中                                      ...  期末                          \\\n",
       "    语文                      数学              ...  数学              英语           \n",
       "    张三 张三丰 张无忌  李四  王五  鲁班  张三 张三丰 张无忌  李四  ... 张无忌  李四  王五  鲁班  张三 张三丰 张无忌   \n",
       "1班  56   0   0  67  53   0  56   0   0  76  ...   0  54  14   0  17   0   0   \n",
       "2班   0  21  55   0   0  68   0  17  23   0  ...  10   0   0  16   0  83  22   \n",
       "\n",
       "                \n",
       "                \n",
       "    李四  王五  鲁班  \n",
       "1班  65  66   0  \n",
       "2班   0   0  48  \n",
       "\n",
       "[2 rows x 36 columns]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.unstack()\n",
    "\n",
    "df.unstack(fill_value=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 5. 聚合操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "DataFrame聚合函数\n",
    "- 求和\n",
    "- 平均值\n",
    "- 最大值\n",
    "- 最小值等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "      <td>28</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>67</td>\n",
       "      <td>76</td>\n",
       "      <td>33</td>\n",
       "      <td>86</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>53</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>31</td>\n",
       "      <td>14</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>68</td>\n",
       "      <td>56</td>\n",
       "      <td>95</td>\n",
       "      <td>78</td>\n",
       "      <td>16</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>21</td>\n",
       "      <td>17</td>\n",
       "      <td>77</td>\n",
       "      <td>42</td>\n",
       "      <td>67</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>55</td>\n",
       "      <td>23</td>\n",
       "      <td>42</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   56  56  58  28   3  17\n",
       "   李四   67  76  33  86  54  65\n",
       "   王五   53  17  43  31  14  66\n",
       "2班 鲁班   68  56  95  78  16  48\n",
       "   张三丰  21  17  77  42  67  83\n",
       "   张无忌  55  23  42  30  10  22"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>67</td>\n",
       "      <td>76</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>53</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    语文  数学  英语\n",
       "张三  56  56  58\n",
       "李四  67  76  33\n",
       "王五  53  17  43"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.loc['1班',  '期中']\n",
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(459)"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.values.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "张三    170\n",
       "李四    176\n",
       "王五    113\n",
       "dtype: int64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.sum()\n",
    "df2.sum(axis=0)  # 求每一列中多行的和\n",
    "df2.sum(axis=1)  # 求每一行中多列的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "语文    67\n",
       "数学    76\n",
       "英语    58\n",
       "dtype: int32"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.mean()\n",
    "df2.max()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "多层索引聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">期中</th>\n",
       "      <th colspan=\"3\" halign=\"left\">期末</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">1班</th>\n",
       "      <th>张三</th>\n",
       "      <td>56</td>\n",
       "      <td>56</td>\n",
       "      <td>58</td>\n",
       "      <td>28</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>67</td>\n",
       "      <td>76</td>\n",
       "      <td>33</td>\n",
       "      <td>86</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>53</td>\n",
       "      <td>17</td>\n",
       "      <td>43</td>\n",
       "      <td>31</td>\n",
       "      <td>14</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"3\" valign=\"top\">2班</th>\n",
       "      <th>鲁班</th>\n",
       "      <td>68</td>\n",
       "      <td>56</td>\n",
       "      <td>95</td>\n",
       "      <td>78</td>\n",
       "      <td>16</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张三丰</th>\n",
       "      <td>21</td>\n",
       "      <td>17</td>\n",
       "      <td>77</td>\n",
       "      <td>42</td>\n",
       "      <td>67</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>张无忌</th>\n",
       "      <td>55</td>\n",
       "      <td>23</td>\n",
       "      <td>42</td>\n",
       "      <td>30</td>\n",
       "      <td>10</td>\n",
       "      <td>22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        期中          期末        \n",
       "        语文  数学  英语  语文  数学  英语\n",
       "1班 张三   56  56  58  28   3  17\n",
       "   李四   67  76  33  86  54  65\n",
       "   王五   53  17  43  31  14  66\n",
       "2班 鲁班   68  56  95  78  16  48\n",
       "   张三丰  21  17  77  42  67  83\n",
       "   张无忌  55  23  42  30  10  22"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "期中  语文    320\n",
       "    数学    245\n",
       "    英语    348\n",
       "期末  语文    295\n",
       "    数学    164\n",
       "    英语    301\n",
       "dtype: int64"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum()  # 默认是求每一列中多行的和\n",
    "df.sum(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1班  张三     218\n",
       "    李四     381\n",
       "    王五     224\n",
       "2班  鲁班     361\n",
       "    张三丰    307\n",
       "    张无忌    182\n",
       "dtype: int64"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sum(axis=1)  # 默认是求每一行中多列的和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sum() got an unexpected keyword argument 'level'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[78], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msum\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlevel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# 表式计算 行 中 的第1层（level=0）\u001b[39;00m\n\u001b[0;32m      2\u001b[0m df\u001b[38;5;241m.\u001b[39msum(axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, level\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)  \u001b[38;5;66;03m# 表式计算 行 中 的第2层（level=1）\u001b[39;00m\n\u001b[0;32m      4\u001b[0m df\u001b[38;5;241m.\u001b[39msum(axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m, level\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m)  \u001b[38;5;66;03m# 表式计算 列 中 的第1层（level=0）\u001b[39;00m\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\core\\frame.py:11670\u001b[0m, in \u001b[0;36mDataFrame.sum\u001b[1;34m(self, axis, skipna, numeric_only, min_count, **kwargs)\u001b[0m\n\u001b[0;32m  11661\u001b[0m \u001b[38;5;129m@doc\u001b[39m(make_doc(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msum\u001b[39m\u001b[38;5;124m\"\u001b[39m, ndim\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m))\n\u001b[0;32m  11662\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msum\u001b[39m(\n\u001b[0;32m  11663\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m  11668\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m  11669\u001b[0m ):\n\u001b[1;32m> 11670\u001b[0m     result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28msuper\u001b[39m()\u001b[38;5;241m.\u001b[39msum(axis, skipna, numeric_only, min_count, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m  11671\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m result\u001b[38;5;241m.\u001b[39m__finalize__(\u001b[38;5;28mself\u001b[39m, method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msum\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\core\\generic.py:12506\u001b[0m, in \u001b[0;36mNDFrame.sum\u001b[1;34m(self, axis, skipna, numeric_only, min_count, **kwargs)\u001b[0m\n\u001b[0;32m  12498\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msum\u001b[39m(\n\u001b[0;32m  12499\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[0;32m  12500\u001b[0m     axis: Axis \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m  12504\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m  12505\u001b[0m ):\n\u001b[1;32m> 12506\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_min_count_stat_function(\n\u001b[0;32m  12507\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msum\u001b[39m\u001b[38;5;124m\"\u001b[39m, nanops\u001b[38;5;241m.\u001b[39mnansum, axis, skipna, numeric_only, min_count, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs\n\u001b[0;32m  12508\u001b[0m     )\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\core\\generic.py:12471\u001b[0m, in \u001b[0;36mNDFrame._min_count_stat_function\u001b[1;34m(self, name, func, axis, skipna, numeric_only, min_count, **kwargs)\u001b[0m\n\u001b[0;32m  12459\u001b[0m \u001b[38;5;129m@final\u001b[39m\n\u001b[0;32m  12460\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_min_count_stat_function\u001b[39m(\n\u001b[0;32m  12461\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m  12468\u001b[0m     \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs,\n\u001b[0;32m  12469\u001b[0m ):\n\u001b[0;32m  12470\u001b[0m     \u001b[38;5;28;01massert\u001b[39;00m name \u001b[38;5;129;01min\u001b[39;00m [\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msum\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mprod\u001b[39m\u001b[38;5;124m\"\u001b[39m], name\n\u001b[1;32m> 12471\u001b[0m     \u001b[43mnv\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvalidate_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m  12473\u001b[0m     validate_bool_kwarg(skipna, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mskipna\u001b[39m\u001b[38;5;124m\"\u001b[39m, none_allowed\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[0;32m  12475\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m axis \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\compat\\numpy\\function.py:418\u001b[0m, in \u001b[0;36mvalidate_func\u001b[1;34m(fname, args, kwargs)\u001b[0m\n\u001b[0;32m    415\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m validate_stat_func(args, kwargs, fname\u001b[38;5;241m=\u001b[39mfname)\n\u001b[0;32m    417\u001b[0m validation_func \u001b[38;5;241m=\u001b[39m _validation_funcs[fname]\n\u001b[1;32m--> 418\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mvalidation_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\compat\\numpy\\function.py:88\u001b[0m, in \u001b[0;36mCompatValidator.__call__\u001b[1;34m(self, args, kwargs, fname, max_fname_arg_count, method)\u001b[0m\n\u001b[0;32m     86\u001b[0m     validate_kwargs(fname, kwargs, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdefaults)\n\u001b[0;32m     87\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m method \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mboth\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m---> 88\u001b[0m     \u001b[43mvalidate_args_and_kwargs\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m     89\u001b[0m \u001b[43m        \u001b[49m\u001b[43mfname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmax_fname_arg_count\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdefaults\u001b[49m\n\u001b[0;32m     90\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     91\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m     92\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minvalid validation method \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mmethod\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\util\\_validators.py:223\u001b[0m, in \u001b[0;36mvalidate_args_and_kwargs\u001b[1;34m(fname, args, kwargs, max_fname_arg_count, compat_args)\u001b[0m\n\u001b[0;32m    218\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[0;32m    219\u001b[0m             \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m() got multiple values for keyword argument \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m    220\u001b[0m         )\n\u001b[0;32m    222\u001b[0m kwargs\u001b[38;5;241m.\u001b[39mupdate(args_dict)\n\u001b[1;32m--> 223\u001b[0m \u001b[43mvalidate_kwargs\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcompat_args\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\util\\_validators.py:164\u001b[0m, in \u001b[0;36mvalidate_kwargs\u001b[1;34m(fname, kwargs, compat_args)\u001b[0m\n\u001b[0;32m    142\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    143\u001b[0m \u001b[38;5;124;03mChecks whether parameters passed to the **kwargs argument in a\u001b[39;00m\n\u001b[0;32m    144\u001b[0m \u001b[38;5;124;03mfunction `fname` are valid parameters as specified in `*compat_args`\u001b[39;00m\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m    161\u001b[0m \u001b[38;5;124;03mmap to the default values specified in `compat_args`\u001b[39;00m\n\u001b[0;32m    162\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m    163\u001b[0m kwds \u001b[38;5;241m=\u001b[39m kwargs\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[1;32m--> 164\u001b[0m \u001b[43m_check_for_invalid_keys\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkwargs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcompat_args\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    165\u001b[0m _check_for_default_values(fname, kwds, compat_args)\n",
      "File \u001b[1;32md:\\Developer\\WGJ\\VSCode_WorkSpace\\Python\\PyEcharts\\.venv\\lib\\site-packages\\pandas\\util\\_validators.py:138\u001b[0m, in \u001b[0;36m_check_for_invalid_keys\u001b[1;34m(fname, kwargs, compat_args)\u001b[0m\n\u001b[0;32m    136\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m diff:\n\u001b[0;32m    137\u001b[0m     bad_arg \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mnext\u001b[39m(\u001b[38;5;28miter\u001b[39m(diff))\n\u001b[1;32m--> 138\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mfname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m() got an unexpected keyword argument \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mbad_arg\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: sum() got an unexpected keyword argument 'level'"
     ]
    }
   ],
   "source": [
    "df.sum(axis=0, level=0)  # 表式计算 行 中 的第1层（level=0）\n",
    "df.sum(axis=0, level=1)  # 表式计算 行 中 的第2层（level=1）\n",
    "\n",
    "df.sum(axis=1, level=0)  # 表式计算 列 中 的第1层（level=0）\n",
    "df.sum(axis=1, level=1)  # 表式计算 列 中 的第2层（level=1）"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
